<?php
require_once("./config.conf.php");
interface ICommand
{
    function onCommand( $name, $args );
}

interface IObserver
{
    function onChanged( $sender, $args );
}

interface IService
{
    public function open(&$message, $args, $env);
    
    public function close(&$message, $args, $env);
    
    public function update(&$message, $args, $env);
    
    public function get(&$message, $args, $env);
}

interface IError
{
    public function getErrNo();
    
    public function getErrMsg();
    
    protected function setErrNo($errNo);
    
    protected function setErrMsg($errMsg); 
}
class ManagerBase implements IObserver, IError
{
    protected $_commands = array();
    
    protected $_observers = array();
    
    protected static $_models = array();
    
    protected static $_libs = array();
    
    protected $errNo = 0;

    protected $errMsg = '';
    
    public function addCommand( $cmd, $key=null )
    {
        if (!$key) {
        	$key = get_class($cmd);
        }
        $this->_commands [$key]= $cmd;
        return true;
    }
    
    public function removeCommand ( $key )
    {
        unset($this->_commands [$key] );
    }
    
    public function runCommand( $name, $args, &$message )
    {
        foreach( $this->_commands as $cmd )
        {
          if ( ! $cmd->onCommand( $name, $args, &$message ) ) {
            return false;
          }
        }
    }
    
    public function addObserver( $observer, $key=null )
    {
        if (!$key) {
        	$key = get_class($observer);
        }
        $this->_observers [$key]= $observer;
        return true;
    }
    
    public function removeObserver ( $key )
    {
        unset($this->_observers [$key] );
    }
    
    public function onChanged( $method, $args, &$message )
    {
        foreach( $this->_observers as $obs ) {
            if (!$obs->$method( $message, $args )) {
            	return false;
            }
        }
               
    }
    
    public static function getModelInc($key)
    {
        if (!isset(self::$_models[$key])) {
        	require_once OPEN_LIB_PATH . '/open/models/'.$key.'.php';
        	self::$_models[$key] = new $key;
        }
        return self::$_models[$key];
    }
    
    public static function loadLibs($key)
    {
        if (!isset(self::$_libs[$key])) {        	
        	self::$_libs[$key] = require  './libs/'.$key.'.php';
        }
    }
    
    public function getErrNo()
    {
        
    }
    
    public function getErrMsg()
    {
        
    }
    
    protected function setErrNo($errNo)
    {
        
    }
    
    protected function setErrMsg($errMsg)
    {
        
    }
}
function loadSdk($key)
{
    static $_sdks = array();
    if (!isset(self::$_sdks[$key])) {        	
    	self::$_sdks[$key] = require_once OA_LIB_PATH . '/inner/service/'.$key.'.php';
    }
}
